
package com.shop.cloud.mall.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cloud.common.core.constant.CommonConstants;
import com.shop.cloud.common.core.constant.SecurityConstants;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.sms.config.SmsConfigProperties;
import com.shop.cloud.common.sms.util.SmsUtils;
import com.shop.cloud.mall.admin.service.ShopInfoService;
import com.shop.cloud.mall.common.constant.MallConstants;
import com.shop.cloud.mall.common.entity.ShopApply;
import com.shop.cloud.mall.admin.mapper.ShopApplyMapper;
import com.shop.cloud.mall.admin.service.ShopApplyService;
import com.shop.cloud.mall.common.entity.ShopInfo;
import com.shop.cloud.upms.common.entity.SysConfigSms;
import com.shop.cloud.upms.common.entity.SysConfigSmsTemplates;
import com.shop.cloud.upms.common.entity.SysUser;
import com.shop.cloud.upms.common.feign.FeignConfigSmsService;
import com.shop.cloud.upms.common.feign.FeignShopUserService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * 店铺入驻申请表
 *
 * @author
 * @date 2020-10-28 10:39:58
 */
@Service
@AllArgsConstructor
public class ShopApplyServiceImpl extends ServiceImpl<ShopApplyMapper, ShopApply> implements ShopApplyService {

	private final ShopInfoService shopInfoService;
	private final FeignShopUserService feignShopUserService;
	private final FeignConfigSmsService feignConfigSmsService;

	@SneakyThrows
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean verify(ShopApply shopApply) {
		String status = shopApply.getStatus();
		String applyDetail = shopApply.getApplyDetail();
		shopApply = baseMapper.selectById(shopApply.getId());
		if(MallConstants.SHOPAPPLY_STATUS_0.equals(shopApply.getStatus())){//只有审核中状态的数据能操作
			shopApply.setStatus(status);
			shopApply.setApplyDetail(applyDetail);
			if(MallConstants.SHOPAPPLY_STATUS_1.equals(status)){//如果审核通过，则需要添加店铺和店员
				//添加店铺
				ShopInfo shopInfo = new ShopInfo();
				BeanUtil.copyProperties(shopApply,shopInfo);
				shopInfo.setTenantId(null);
				shopInfo.setEnable(CommonConstants.YES);
				shopInfo.setSaleType(MallConstants.SHOP_SALETYPE_1);
				shopInfoService.save(shopInfo);
				shopApply.setShopId(shopInfo.getId());
				//更新状态
				baseMapper.updateById(shopApply);
				SysUser sysUser = new SysUser();
				sysUser.setShopId(shopInfo.getId());
				sysUser.setUsername(shopApply.getUserUsername());
				sysUser.setPassword(shopApply.getUserPassword());
				sysUser.setPhone(shopApply.getUserPhone());
				sysUser.setEmail(shopApply.getUserEmail());
				sysUser.setAvatar(shopApply.getUserAvatar());
				sysUser.setNickName(shopApply.getUserNickname());
				//新增店员
				R r = feignShopUserService.save(sysUser, SecurityConstants.FROM_IN);
				if(!r.isOk()){
					throw new RuntimeException(r.getMsg());
				}
				try{
					//发送短信通知
					Map map = new HashMap<>();
					map.put("shopName", shopInfo.getName());
					map.put("mallName", shopApply.getMallName());
					map.put("userName", sysUser.getUsername());
					SysConfigSms sysConfigSms = feignConfigSmsService.getSysConfigSms(SecurityConstants.FROM_IN).getData();
					if(sysConfigSms == null){
						throw new RuntimeException("未配置短信账号");
					}
					SysConfigSmsTemplates sysConfigSmsTemplates = JSONUtil.toBean(sysConfigSms.getTemplates(),SysConfigSmsTemplates.class);
					SmsConfigProperties smsConfigProperties = new SmsConfigProperties();
					BeanUtil.copyProperties(sysConfigSms, smsConfigProperties);
					SmsUtils.sendSms(smsConfigProperties,
							sysConfigSmsTemplates.getSignName4(),
							sysUser.getPhone(),
							sysConfigSmsTemplates.getTemplateCode4(),
							JSONUtil.toJsonStr(map)
					);
				}catch (Exception e){
					log.error(e.getMessage());
				}
			}else{
				baseMapper.updateById(shopApply);
			}
		}
		return Boolean.TRUE;
	}
}
